const {
    getUserByMongo,   //从Mogo中查询用户信息
    checkUserCode,    //从redis中验证查询验证码
    checkRegisterLegal,   //查看注册是否合法
    initUserDatabase,   //用户注册时初始化相关的数据库表
    loginGetUser,    //登陆后获取用户
    loginInitData,    //客户登陆后初始化数据
    sendPhoneCode,    //发送短信验证码
    sendManagerPlatform, //给运营平台发送
  updateContactInfo,    //设置用户联系方式
  getContactInfo,   //获取用户签名
} = require('../lib/action');
const {
    Msg,
    Code,
} = require('../lib/consts');

const httpRequest = require('request');

module.exports = function (app) { return new Handler(app); };
const Handler = function (app) { this.app = app; this.httpRequest = httpRequest; };
const handler = Handler.prototype;

// 登陆
handler.login = async function (msg, session, next) {
    const {uid} = session;
    try {
        //获取当前用户
        let user = await loginGetUser(uid);
        //客户登陆后初始化数据
        let userInfo = await loginInitData.call(this, session, user);
        //将用户信息赋值回login服务器内存
        this.app.rpc.login.loginRemote.setUser(session, userInfo, function (err, user) {
        });
        //给运营平台发送登陆通知
        sendManagerPlatform.call(this, uid)

        next(null, {code: Code.SUC, userInfo});
    } catch (err) {
        if (err.message == Msg.USER_NOT_REGISTER)    //用户登陆失败后转到注册页面
            next(err, {code: Code.NOT_REGISTER, msg: err.message});
        next(err, {code: Code.ERR, msg: err.message});
    }
};

// 注册
handler.register = async function (msg, session, next) {
    const {uid} = session;
    const ip = session.get('ip');
    const {union_id, nickname, avatar, gender, city, province, phone, code} = msg;
    try {
        //查看注册是否合法
        await checkRegisterLegal.call(this, uid, phone, code);
        //用户注册时初始化相关的数据库表
        let user = await initUserDatabase.call(this, Object.assign(msg, {uid, session}));
        //客户登陆后初始化数据
        let userInfo = await loginInitData.call(this, session, user);
        //将用户信息赋值回login服务器内存
        this.app.rpc.login.loginRemote.setUser(session, userInfo, function (err, user) {
        });

        next(null, {code: Code.SUC, userInfo});
    } catch (err) {
        next(err, {code: Code.ERR, msg: err.message});
    }
};

// 发送验证码
handler.sendCode = async function (msg, session, next) {
  let self = this;
    const {uid} = session;
    const {phone} = msg;
    try {
        //发送短信验证码
        let code = await sendPhoneCode.call(this, uid, phone);
        self.httpRequest({
            url: 'http://39.98.74.164:9292/api/oil/user/send/sms?phone=' + phone + '&code=' + code,
            method: "GET"
        }, function (error, response, body) {
            if (response && response.statusCode != 200) {
                console.error('sendCode 执行失败 response.statusCOde' + response.statusCode)
                console.error('sendCode 执行失败 error' + error)
                return;
            } else {
            }
        });
        next(null, {code: Code.SUC});
    } catch (err) {
        next(err, {code: Code.ERR, msg: err.message});
    }
};

//修改用户签名
handler.updateContactInfo = async function (msg, session, next) {
  try {
    await updateContactInfo.call(this, session, msg);
    next(null, {code: Code.SUC});
  } catch (err) {
    next(err, {code: Code.ERR, msg: err.message});
  }
}

//获取用户签名
handler.getContactInfo = async function (msg, session, next) {
  try {
    let res = await getContactInfo.call(this, session, msg);
    next(null, {code: Code.SUC, ...res});
  } catch (err) {
    next(err, {code: Code.ERR, msg: err.message});
  }
}
